package satir.card.factory;

import org.json.JSONArray;
import org.json.JSONObject;
import satir.card.Deck;
import satir.framework.refectoring.Factory;

/**
 * A factory for creating Deck objects.
 * @version 3.0 April 2012
 * @author Creative Family
 */
public class DeckFactory implements Factory {

    /** The card json. */
    protected JSONObject json;

    /**
     * Creates the.
     * @param json
     *            the JSON object
     * @return the object
     * @see Factory, LazyInitiator
     */
    @Override
    public Object create(final JSONObject json) {

	Deck concreteProduct = new Deck();
	try {
	    String cardType = json.getString("key");
	    JSONArray cardKeysJSArr = json.getJSONArray("cardKeys");
	    String[] cardKeys = new String[cardKeysJSArr.length()];
	    for (int i = 0; i < cardKeysJSArr.length(); i++) {
		cardKeys[i] = cardKeysJSArr.getString(i);
	    }
	    concreteProduct.loadCard(cardType, cardKeys);
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return concreteProduct;
    }
}
